package com.hoshiibuy.brandcategory.service.impl;

import com.hoshiibuy.brandcategory.entity.GoodsPropertiesValues;
import com.hoshiibuy.brandcategory.mapper.GoodsPropertiesValuesMapper;
import com.hoshiibuy.brandcategory.service.GoodsPropertiesValuesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hoshiibuy.common.aop.Logable;
import com.hoshiibuy.common.exception.CommonError;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.brandcategory.feign.IdGeneratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;

/**
 * <p>
 * 商品属性_值 服务实现类
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-04-30
 */
@Service
public class GoodsPropertiesValuesServiceImpl extends ServiceImpl<GoodsPropertiesValuesMapper, GoodsPropertiesValues> implements GoodsPropertiesValuesService {

    @Autowired
    private IdGeneratorService idGeneratorService;

    /**
    * @param id
    * @return com.hoshiibuy.brandcategory.entity.GoodsPropertiesValues
    * @description: 根据id查询商品属性_值
    */
    @Cacheable(cacheNames = "GoodsPropertiesValues", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "商品类目及品牌管理-商品属性_值 -查询商品属性_值 ")
    public GoodsPropertiesValues getById(Long id) {
        return baseMapper.selectById(id);
    }


    /**
    * @param entity
    * @return boolean
    * @description: 新增商品属性_值
    */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "GoodsPropertiesValues", allEntries = true)
    @Logable(authName = "商品类目及品牌管理-商品属性_值-新增商品属性_值")
    public boolean save(GoodsPropertiesValues entity) {
        String id = idGeneratorService.queryGeneratorId();
        if (id == null || id == "") {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        entity.setId(Long.parseLong(id));
        entity.setCreatedtime(LocalDateTime.now());
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return true;
    }

    /**
    * @param entity
    * @return boolean
    * @description: 修改商品属性_值
    */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "GoodsPropertiesValues", allEntries = true)
    @Logable(authName = "商品类目及品牌管理-商品属性_值管理-修改商品属性_值")
    public boolean updateById(GoodsPropertiesValues entity) {
        entity.setUpdatedtime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }

    /**
    * @param queryWrapper
    * @return IPage
    * @description: 分页查询商品属性_值
    */
    @Logable(authName = "商品类目及品牌管理-商品属性_值管理-分页查询商品属性_值")
    @Cacheable(cacheNames = "GoodsPropertiesValues", key = "T(com.hoshiibuy.common.utils.DigestUtils).md5('Page(current=' + #page.getCurrent() + ', size=' + #page.getSize() + '), Wrapper(SqlSelect=' + #queryWrapper.getSqlSelect() + ', SqlSegment=' + #queryWrapper.getSqlSegment() + ')')")
    public IPage<GoodsPropertiesValues> page(IPage<GoodsPropertiesValues> page, Wrapper<GoodsPropertiesValues> queryWrapper) {
        return baseMapper.selectPage(page, queryWrapper);
    }
}
